package judesart.app.client.user.ui;

import java.util.List;

import judesart.app.client.user.UserControllerAsync;
import judesart.app.domain.User;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class UserLoaderProxy extends RpcProxy<PagingLoadResult<UserModel>>
{
    UserControllerAsync userController;

    public UserLoaderProxy(UserControllerAsync userController)
    {
        this.userController = userController;
    }

    @Override
    protected void load(Object loadConfig,
            final AsyncCallback<PagingLoadResult<UserModel>> callback)
    {
        userController.searchUsers(new AsyncCallback<List<User>>()
        {
            @Override
            public void onFailure(Throwable caught)
            {
                callback.onFailure(caught);
            }

            public void onSuccess(List<User> result)
            {
                List<UserModel> list = UserModel.createModel(result);
                PagingLoadResult<UserModel> loadResults = new BasePagingLoadResult<UserModel>(
                        list, 0, result.size());
                callback.onSuccess(loadResults);
            };
        });
    }
}
